package com.sxt.study.luckymoney.controller;

import com.sxt.study.luckymoney.model.LuckyMoney;
import com.sxt.study.luckymoney.repository.LuckyMoneyRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author songj
 * @date 2019/7/23 17:59
 */
@Api(value = "红包管理", description = "红包管理服务")
@RestController
public class LuckyMoneyController {

    @Autowired
    private LuckyMoneyRepository luckyMoneyRepository;

    @ApiOperation(value = "查看所有红包", notes = "查看所有红包")
    @GetMapping("/lucky-moneys")
    public List<LuckyMoney> getAll() {
        return luckyMoneyRepository.findAll();
    }

    @ApiOperation(value = "查看单个红包", notes = "查看单个红包")
    @GetMapping("/lucky-money/{id}")
    public LuckyMoney getById(@PathVariable("id") Long id) {
        return luckyMoneyRepository.findById(id).orElse(new LuckyMoney());
    }

    @ApiOperation(value = "发红包", notes = "发红包")
    @PostMapping("/lucky-money")
    public LuckyMoney sendLuckyMoney(BigDecimal amount, String sender) {
        LuckyMoney luckyMoney = new LuckyMoney();
        luckyMoney.setAmount(amount);
        luckyMoney.setSender(sender);
        return luckyMoneyRepository.save(luckyMoney);
    }

    @ApiOperation(value = "点红包", notes = "点红包")
    @PutMapping("/lucky-money")
    public LuckyMoney receiveLuckyMoney(Long id, String consumer) {
        LuckyMoney luckyMoney = luckyMoneyRepository.getOne(id);
        luckyMoney.setConsumer(consumer);
        return luckyMoneyRepository.save(luckyMoney);
    }

    @ApiOperation(value = "撤回红包", notes = "撤回红包")
    @DeleteMapping("/lucky-money/{id}")
    public void cancelLuckyMoney(@PathVariable("id") Long id) {
        luckyMoneyRepository.deleteById(id);
    }
}
